﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ASPNETMVCApplication.ActionServiceReference;
using ASPNETMVCApplication.Repositories.Core;

namespace ASPNETMVCApplication.Repositories
{
    public class TrafficRepository : RepositoryBase, ITrafficRepository
    {
        /// <summary>
        /// Gets list of Traffics.
        /// </summary>
        /// <param name="criterion"></param>
        /// <returns></returns>
        public List<Traffic> GetList(Criterion criterion = null)
        {
            var request = new TrafficRequest().Prepare();

            request.LoadOptions = new string[] { "Traffics" };
            request.Criteria = new TrafficCriteria
            {
                //TrafficId = int.Parse(criterion.Filters.Single(f => f.Attribute == "TrafficId").Operand.ToString()),
                TrafficId=0,
                SortExpression = "TrafficId"
            };

            var response = Client.GetTraffics(request);

            Correlate(request, response);

            return response.Traffics == null ? null : response.Traffics.ToList();
        }
        public List<Traffic> Search(int supplierID, int trafficID, string sort, string order)
        {
            var request = new TrafficRequest().Prepare();

            request.LoadOptions = new string[] { "Search" };

            request.Criteria = new TrafficCriteria
            {
                TrafficId = trafficID,
                SupplierID = supplierID,
                SortExpression = sort + " " + order
            };

            var response = Client.GetTraffics(request);

            Correlate(request, response);

            return response.Traffics.ToList();
        }
        public Traffic Get(int supplierID, int trafficID)
        {
            var request = new TrafficRequest().Prepare();
            request.LoadOptions = new string[] { "Traffic" };
            request.Criteria = new TrafficCriteria {  SupplierID = supplierID, TrafficId = trafficID };

            var response = Client.GetTraffics(request);

            Correlate(request, response);

            return response.Traffic;
        }
        public void Insert(Traffic traffic)
        {
            var request = new TrafficRequest().Prepare();
            request.Action = "Create";
            request.Traffic = traffic;

            var response = Client.SetTraffics(request);

            Correlate(request, response);

            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
        }
        public void Update(Traffic traffic)
        {
            var request = new TrafficRequest().Prepare();
            request.Action = "Update";
            request.Traffic = traffic;

            var response = Client.SetTraffics(request);

            Correlate(request, response);

            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
        }

        

        #region Not implemented members
        public Traffic Get(int _trafficID)
        {
            throw new NotImplementedException();
        }
        public List<Traffic> Search(int _trafficID)
        {
            throw new NotImplementedException();
        }
        public int GetCount(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }
        public void Delete(int productId)
        {
            throw new NotImplementedException();
        }
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}